﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.DAL;

namespace PerfumeShopManagement.BLL
{
    public class BllCategory : BllAbstractBase<Category, int>
    {
        public BllCategory() 
            : base()
        {
            items = DalFactory.CategoryDAL.GetAllObjects();
        }

        public override List<Category> GetAll()
        {
            return items;
        }

        public override Category Get(int id)
        {
            throw new NotImplementedException();
        }

        public override List<Category> Search(Category criteria)
        {
            return DalFactory.CategoryDAL.GetObjectsByCriteria(criteria);
        }

        public override bool Save(Category obj)
        {
            Category category = obj;
            bool updated = false;
            foreach (Category item in items)
            {
                if (item.CategoryId == category.CategoryId)
                {
                    item.CategoryName = category.CategoryName;
                    updated = true;
                    DalFactory.CategoryDAL.Update(category);

                    return true;
                }
            }

            if (!updated)
            {
                items.Add(category);
                DalFactory.CategoryDAL.Insert(category);

                return true;
            }

            return false;
        }

        public override bool DeleteById(int id)
        {
            if (DalFactory.CategoryDAL.DeleteById(id))
            {
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    if (id == items[i].CategoryId)
                        items.Remove(items[i]);
                }
                return true;
            }
            return false;
        }
    }
}
